/**
 * 
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Country;
import org.cuatrovientos.erp.model.Employee;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author pablo_guerrero
 * This class implements CountryDAOInterface
 *
 */
public class CountryDAO implements CountryDAOInterface{

	@Autowired
	 private SessionFactory sessionFactory;
	
	/**
	 * default Constructor
	 */
	public CountryDAO(){
		
	}
	
	/**
	 * returns Hibernate session
	 * @return the session
	 */
	 private Session getSession(){
		 return sessionFactory.getCurrentSession();
	 }
	 
	/**
	 * @param int id
	 * @return Country country
	 * This method returns a country searched by it's id
	 */
	@Override
	@Transactional(readOnly = true)
	public Country selectById(int id) {
		Country country = (Country) getSession().get(Country.class, id);
	    return country;
	}

	/**
	 * @return All the countries we have in the database
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Country> selectAll() {
		List<Country> countries = getSession().createQuery("from Country").list();
	    return countries;
	}

	/**
	 * @return The number of countries we have in the database
	 */
	@Override
	@Transactional(readOnly = true)
	public int selectAllCount() {
		List<Country> countries = getSession().createQuery("from Country").list();
	    return countries.size();
	}

	/**
	 * 
	 */
	@Override
	@Transactional
	public void saveOrUpdate(Country country) {
		getSession().saveOrUpdate(country);
		
	}

	/**
	 * @param Country country
	 * This method insert a country in the database passed by parameter
	 */
	@Override
	@Transactional
	public void insert(Country country) {
		Integer id = (Integer) getSession().save(country);
		country.setId(id);
	}

	/**
	 * @param Country country
	 * This method updates a country passed by parameter
	 */
	@Override
	@Transactional
	public void update(Country country) {
		getSession().merge(country);
	}

	/**
	 * @param Country country
	 * This method deletes a country passed by parameter
	 */
	@Override
	@Transactional
	public void delete(Country country) {
		getSession().delete(country);
		
	}

	/**
	 * @param int id
	 * This method deletes the country selected by id
	 */
	@Override
	@Transactional(readOnly=true)
	public void deleteById(int id) {
		Country country = (Country) getSession().load(Country.class,id);
		getSession().delete(country);
	}

	/**
	 * @param  String name
	 * This method searches all the countries which has the same name we are looking for.
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Country> search(String name) {
		Query query = getSession().createQuery("from Country c where c.name like :name");
        query.setParameter("name", "%"+name+"%");
        List<Country> countries = query.list();
		
        return countries;
	}

}
